/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);
    const self = this;
    let regionCodeValue = '';
    getUserRegionCode().then(regionCode=>{
        regionCodeValue = regionCode;
        getStallTotal(self, regionCode);
        getStallAppointementTotal(self, regionCode);
        getStallOrganizationTotal(self, regionCode);
    })


    // 页面配置映射
    const PAGE_CONFIG = {
        report:        { id: "1957706382569373696", title: "检查上报" },
        organization:  { id: "1958011351386292224", title: "场所管理" },
        stall:         { id: "1958053267519832064", title: "摊位管理" },
        holder:        { id: "1958010837143650304", title: "摊主管理" }
    };

    // 按钮与页面的对应关系
    const BUTTON_ACTIONS = {
        "btn-check":        PAGE_CONFIG.report,
        "btn-organization": PAGE_CONFIG.organization,
        "btn-stall":        PAGE_CONFIG.stall,
        "btn-holder":       PAGE_CONFIG.holder
    };

    // 批量绑定点击事件
    Object.entries(BUTTON_ACTIONS).forEach(([btnId, page]) => {
        const btn = document.getElementById(btnId);
        if (!btn) return;

        btn.addEventListener("click", () => {
            openPage(self, page.id, page.title, regionCodeValue);
        });
    });
}

/**
 * 获取摊主和场所统计数
 */
function getStallOrganizationTotal(self, regionCodeValue){

    const params = [{
        name: "regionCode",
        valueContent: regionCodeValue
    }];

    api.DATA_SOURCE_EXECUTE("ds_mod:24d3fdbb-8e8f-45bd-9c90-4b02970fe7ae", "ddcat", params, [], "").then(rs => {
        let holder_count = parseInt(rs.result[0].count);
        let organization_count = parseInt(rs.result[1].count);

        self.$$("text_ajoz99").$$setValue(holder_count  + '');
        self.$$("text_6w9n06").$$setValue(organization_count  + '');

        console.log(rs)
    });

}

/**
 * 获取摊位数据
 */
function getStallAppointementTotal(self, regionCodeValue){

    const params = [{
        name: "regionCode",
        valueContent: regionCodeValue
    }];

    api.DATA_SOURCE_EXECUTE("ds_mod:c6df341a-d082-431f-a5c5-cd35a07d4d68", "ddcat", params, [], "").then(rs => {
        let all_stall_total = parseInt(rs.result[0].total_count);
        let appointement_stall_total = parseInt(rs.result[1].total_count);

        let stall_count = appointement_stall_total + '/' + all_stall_total

        self.$$("text_4507k0").$$setValue(stall_count  + '');

        console.log(rs)
    });

}

/**
 * 获取数据概览
 */
function getStallTotal(self, regionCodeValue){

    const params = [{
        name: "regionCode",
        valueContent: regionCodeValue
    }];

    api.DATA_SOURCE_EXECUTE("ds_mod:ea0e8c3d-d0d7-4259-ac44-7129967b0e00", "ddcat", params, [], "").then(rs => {
        let today_total_all = parseInt(rs.result[0].today_total_all) || 0;
        let today_un_stall_total = parseInt(rs.result[0].today_un_stall_total) || 0;
        let today_stall_total = parseInt(rs.result[0].today_stall_total) || 0;
        let total_all = parseInt(rs.result[0].total_all) || 0;

        self.$$("text_57u71z").$$setValue(today_total_all + '');
        self.$$("text_6urh9t").$$setValue(today_un_stall_total  + '');
        self.$$("text_ehiflh").$$setValue(today_stall_total  + '');
        self.$$("text_13906c").$$setValue(total_all  + '');

        console.log(rs)
    });
}

/**
 * 打开页面
 */
function openPage(self, pageId, title, params, regionCode) {
    self.$router.push({
        name: "page-preview",
        query: {
            title,
            pageId,
            params,
            showHeader: self.$route.query.showHeader,
            regionCode: regionCode,
            pageTitle: title,
            linkTitle: title,
            showBackHeader: true,
            isFromAction: 1,
            _t: Date.now().toString()
        }
    });
}


/**
 * 获取当前登陆人员所属部门的所属区域
 */

function getUserRegionCode(){
    return new Promise((resolve, reject) => {
        api.DATA_SOURCE_EXECUTE("ds_mod:cb92dffe-47b9-4552-87e0-75d1b7228a7b", "ddcat", [], [], "").then(rs => {
            resolve(rs.result[0].processedRegionCode);
        });
    });
}
